import json
import re
import sys

# NOTE: Get station data from:
# https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9053

def extract_data(input_file, output_json):
	with open(input_file, 'r', encoding='utf-8') as file:
		content = file.read()

	pattern = r'@([^|]+)\|([^|]+)\|([^|]+)\|'
	matches = re.findall(pattern, content)

	result = {match[1]: match[2] for match in matches}
	
	with open(output_json, 'w', encoding='utf-8') as json_file:
		json.dump(result, json_file, ensure_ascii=False, indent=4)

if __name__ == "__main__":
	# Read command line arguments
	if len(sys.argv) < 2:
		print("Usage: python UpdateStations.py <stations_file>")
		exit(1)
	# Replace the extension of the input file with ".json"
	output_json = sys.argv[1].replace(".txt", ".json")
	extract_data(sys.argv[1], output_json)
	print(f"Data has been extracted to {output_json}.")
